package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;
import com.future.mayi.adapter.CouponsAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.CouponsModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.CouponsResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectCouponsActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.iv_null)
    ImageView ivNull;
    @BindView(R.id.tv_enter)
    TextView tvEnter;


    private int type = 2;

    private double money;
    private String goods_ids;
    private CouponsAdapter adapter;


    public static void startActivity(Activity activity, double money, String goods) {
        activity.startActivityForResult(new Intent(activity, SelectCouponsActivity.class).putExtra("money", money).putExtra("goods", goods), 235);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("选择优惠券");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_coupons_select);
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new CouponsAdapter(this, recyclerView, type);
        recyclerView.setAdapter(adapter);
        recyclerView.setEmptyView(ivNull);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {
        money = getIntent().getDoubleExtra("money", 0);
        goods_ids = getIntent().getStringExtra("goods");
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
        recyclerView.refresh();
    }

    @SuppressWarnings("unchecked")
    private void getData() {
        new HttpRequest<CouponsResponse>()
                .with(this)
                .setApiCode(ApiCst.getCouponList)
                .addParam("status", 0)
                .addParam("p", 0)
                .addParam("psize", 10000)
                .addParam("token", App.getToken(mContext))
                .addParam("goods_ids", goods_ids)
                .setListener(new HttpRequest.OnNetworkListener<CouponsResponse>() {
                    @Override
                    public void onSuccess(CouponsResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), 0, 10);
                    }

                    @Override
                    public void onFail(String message, CouponsResponse response) {
                        HCUtils.refreshFail(recyclerView, 0, mContext, message);
                    }
                }).start(new CouponsResponse());


    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {
    }

    @Override
    public void OnItem(View view, int position) {
        if (adapter.getItem(position) == null) return;
        if (adapter.getItem(position).getUse_condition() > money) {
            ToastUtils.shortToast(this, "不满足使用条件！");
            return;
        }
        for (int i = 0; i < adapter.getItemCount(); i++) {
            CouponsModel model = adapter.getItem(i);
            if (model == null) continue;
            model.setSelect(i == position);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        CouponsModel couponsModel = null;
        for (int i = 0; i < adapter.getItemCount(); i++) {
            CouponsModel model = adapter.getItem(i);
            if (model == null) continue;
            if (model.isSelect()) {
                couponsModel = model;
                break;
            }
        }
        Intent intent = new Intent();
        if (couponsModel != null)
            intent.putExtra("coupons", couponsModel);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
